{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-11-03T02:05:25.157820Z",
     "start_time": "2025-11-03T02:05:25.152957Z"
    }
   },
   "source": "import numpy as np",
   "outputs": [],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-03T02:05:25.189498Z",
     "start_time": "2025-11-03T02:05:25.184153Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 构造特征\n",
    "a = np.random.randn(100)\n",
    "print(np.var(a))"
   ],
   "id": "a50a63b472e3c873",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.8090857051719459\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-03T02:05:25.219017Z",
     "start_time": "2025-11-03T02:05:25.213347Z"
    }
   },
   "cell_type": "code",
   "source": [
    "b = np.random.randn(100) * 0.1\n",
    "b = np.random.normal(5, 0.1, size=100)\n",
    "print(np.var(b))"
   ],
   "id": "20026cc2e3f18bc6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.008586696039984649\n"
     ]
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-03T02:05:25.251006Z",
     "start_time": "2025-11-03T02:05:25.244455Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 构造特征向量\n",
    "X = np.vstack((a, b)).T\n",
    "print(X)\n",
    "print(X.shape)"
   ],
   "id": "e9b8059589555742",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2.07511477e-01  4.81889691e+00]\n",
      " [-1.53534450e+00  5.09428245e+00]\n",
      " [-1.04282203e+00  4.83523529e+00]\n",
      " [-5.82999473e-01  5.01158161e+00]\n",
      " [-2.99451044e+00  4.97163604e+00]\n",
      " [-2.17808904e-01  5.18345245e+00]\n",
      " [-1.27196336e+00  4.97669235e+00]\n",
      " [-4.23342866e-01  4.93881293e+00]\n",
      " [ 6.17236817e-02  4.90141067e+00]\n",
      " [ 8.33561599e-02  4.98461866e+00]\n",
      " [ 2.89435238e-01  4.92113933e+00]\n",
      " [ 8.26505317e-02  4.94055108e+00]\n",
      " [ 1.66746846e-02  4.94631041e+00]\n",
      " [-1.21214309e+00  4.96212719e+00]\n",
      " [-1.22274861e+00  4.83343436e+00]\n",
      " [ 1.11013370e+00  4.95406524e+00]\n",
      " [-9.86053709e-01  5.02843855e+00]\n",
      " [ 1.70558348e+00  4.79718994e+00]\n",
      " [ 9.02091221e-01  4.96943615e+00]\n",
      " [-1.23116523e+00  4.89686026e+00]\n",
      " [-1.59508093e+00  5.04062960e+00]\n",
      " [-3.30485431e-01  5.06730193e+00]\n",
      " [ 2.33146643e-01  5.04603835e+00]\n",
      " [ 2.78841415e-01  5.13398561e+00]\n",
      " [-3.08470255e-01  4.87924480e+00]\n",
      " [ 9.18933021e-01  4.87577271e+00]\n",
      " [-8.53174898e-01  5.05777838e+00]\n",
      " [-1.05043704e+00  5.05494464e+00]\n",
      " [-3.77891119e-01  5.10034447e+00]\n",
      " [-1.08087912e+00  5.08080837e+00]\n",
      " [ 1.75716564e-01  4.81649990e+00]\n",
      " [ 9.41543717e-02  5.11158397e+00]\n",
      " [ 7.61736472e-01  5.06607249e+00]\n",
      " [ 9.02431129e-01  4.97870876e+00]\n",
      " [-6.34480484e-01  5.00419183e+00]\n",
      " [-2.68820551e-01  5.08928577e+00]\n",
      " [-4.53995570e-02  4.94362302e+00]\n",
      " [ 8.07335943e-01  4.96670483e+00]\n",
      " [-6.95737511e-01  4.89523791e+00]\n",
      " [ 2.81087663e-01  4.85334029e+00]\n",
      " [ 8.52362392e-01  4.83898667e+00]\n",
      " [-2.94645023e-01  4.94739203e+00]\n",
      " [-7.35816651e-01  5.02634422e+00]\n",
      " [ 5.37569406e-01  4.91294309e+00]\n",
      " [ 1.51390575e-01  4.99199070e+00]\n",
      " [ 4.35099055e-01  4.79148244e+00]\n",
      " [ 1.63588101e-01  4.93346756e+00]\n",
      " [ 1.17587627e+00  4.96578832e+00]\n",
      " [ 4.31392983e-01  4.82317984e+00]\n",
      " [-3.13902292e-01  4.83610242e+00]\n",
      " [-4.34890620e-01  5.10335863e+00]\n",
      " [-5.95657993e-02  4.88153738e+00]\n",
      " [-4.70112896e-01  4.88275435e+00]\n",
      " [-4.01679859e-01  5.00558892e+00]\n",
      " [-6.57128062e-01  4.96161276e+00]\n",
      " [ 2.08695266e+00  5.01427300e+00]\n",
      " [ 1.67661743e+00  5.06296543e+00]\n",
      " [ 5.83901413e-01  4.94363592e+00]\n",
      " [-5.94489232e-01  5.20364990e+00]\n",
      " [-5.79233599e-01  5.09514018e+00]\n",
      " [ 2.25829554e-01  5.09906814e+00]\n",
      " [ 2.53569535e-01  4.98401639e+00]\n",
      " [-3.63485718e-01  4.95858066e+00]\n",
      " [ 6.65615481e-01  5.00029132e+00]\n",
      " [-5.53293776e-01  5.00560795e+00]\n",
      " [-2.00145304e+00  5.03041331e+00]\n",
      " [-5.85029536e-01  4.97412328e+00]\n",
      " [-1.91797646e-03  5.08882013e+00]\n",
      " [ 1.51719446e+00  5.04528102e+00]\n",
      " [-3.84870717e-01  4.98284246e+00]\n",
      " [ 3.89735650e-01  5.05546043e+00]\n",
      " [ 8.40673613e-02  5.12027229e+00]\n",
      " [ 4.65837205e-01  5.13164617e+00]\n",
      " [-4.42296261e-02  5.10465002e+00]\n",
      " [-2.32728399e-01  4.85533005e+00]\n",
      " [ 7.28889113e-01  5.02809234e+00]\n",
      " [ 1.22027297e+00  5.01889917e+00]\n",
      " [-1.30336319e+00  5.02463631e+00]\n",
      " [-4.54013408e-01  4.77769852e+00]\n",
      " [-1.93169503e-01  4.97370495e+00]\n",
      " [-1.27963203e+00  5.01540646e+00]\n",
      " [ 1.42976314e+00  4.97903244e+00]\n",
      " [ 8.35913580e-02  5.01316365e+00]\n",
      " [ 1.85172769e+00  5.08819802e+00]\n",
      " [ 1.90477523e-01  4.93156642e+00]\n",
      " [-6.32126916e-01  5.11846023e+00]\n",
      " [-2.45093741e+00  4.87647026e+00]\n",
      " [ 4.29641028e-01  5.03615511e+00]\n",
      " [ 2.88100470e-01  5.05975648e+00]\n",
      " [ 7.38323027e-01  4.86442079e+00]\n",
      " [ 1.03458837e+00  5.05515810e+00]\n",
      " [-9.42213135e-01  5.04391484e+00]\n",
      " [-7.02500288e-01  4.93182101e+00]\n",
      " [ 8.13058962e-01  5.03588236e+00]\n",
      " [-3.15333631e-01  4.96455628e+00]\n",
      " [-1.75404975e+00  5.06998223e+00]\n",
      " [-3.68679987e-01  5.00203501e+00]\n",
      " [ 7.74244139e-01  4.87462928e+00]\n",
      " [ 1.14095347e-01  4.92016820e+00]\n",
      " [ 3.47748416e-01  4.90959674e+00]]\n",
      "(100, 2)\n"
     ]
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-03T02:05:25.281003Z",
     "start_time": "2025-11-03T02:05:25.274452Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 低方差过滤器\n",
    "from sklearn.feature_selection import VarianceThreshold\n",
    "\n",
    "# 将方差小于0.01的列过滤掉\n",
    "selector = VarianceThreshold(0.01)\n",
    "X_new = selector.fit_transform(X)\n",
    "print(X_new)\n",
    "print(X_new.shape)"
   ],
   "id": "c884113bc3ed2000",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2.07511477e-01]\n",
      " [-1.53534450e+00]\n",
      " [-1.04282203e+00]\n",
      " [-5.82999473e-01]\n",
      " [-2.99451044e+00]\n",
      " [-2.17808904e-01]\n",
      " [-1.27196336e+00]\n",
      " [-4.23342866e-01]\n",
      " [ 6.17236817e-02]\n",
      " [ 8.33561599e-02]\n",
      " [ 2.89435238e-01]\n",
      " [ 8.26505317e-02]\n",
      " [ 1.66746846e-02]\n",
      " [-1.21214309e+00]\n",
      " [-1.22274861e+00]\n",
      " [ 1.11013370e+00]\n",
      " [-9.86053709e-01]\n",
      " [ 1.70558348e+00]\n",
      " [ 9.02091221e-01]\n",
      " [-1.23116523e+00]\n",
      " [-1.59508093e+00]\n",
      " [-3.30485431e-01]\n",
      " [ 2.33146643e-01]\n",
      " [ 2.78841415e-01]\n",
      " [-3.08470255e-01]\n",
      " [ 9.18933021e-01]\n",
      " [-8.53174898e-01]\n",
      " [-1.05043704e+00]\n",
      " [-3.77891119e-01]\n",
      " [-1.08087912e+00]\n",
      " [ 1.75716564e-01]\n",
      " [ 9.41543717e-02]\n",
      " [ 7.61736472e-01]\n",
      " [ 9.02431129e-01]\n",
      " [-6.34480484e-01]\n",
      " [-2.68820551e-01]\n",
      " [-4.53995570e-02]\n",
      " [ 8.07335943e-01]\n",
      " [-6.95737511e-01]\n",
      " [ 2.81087663e-01]\n",
      " [ 8.52362392e-01]\n",
      " [-2.94645023e-01]\n",
      " [-7.35816651e-01]\n",
      " [ 5.37569406e-01]\n",
      " [ 1.51390575e-01]\n",
      " [ 4.35099055e-01]\n",
      " [ 1.63588101e-01]\n",
      " [ 1.17587627e+00]\n",
      " [ 4.31392983e-01]\n",
      " [-3.13902292e-01]\n",
      " [-4.34890620e-01]\n",
      " [-5.95657993e-02]\n",
      " [-4.70112896e-01]\n",
      " [-4.01679859e-01]\n",
      " [-6.57128062e-01]\n",
      " [ 2.08695266e+00]\n",
      " [ 1.67661743e+00]\n",
      " [ 5.83901413e-01]\n",
      " [-5.94489232e-01]\n",
      " [-5.79233599e-01]\n",
      " [ 2.25829554e-01]\n",
      " [ 2.53569535e-01]\n",
      " [-3.63485718e-01]\n",
      " [ 6.65615481e-01]\n",
      " [-5.53293776e-01]\n",
      " [-2.00145304e+00]\n",
      " [-5.85029536e-01]\n",
      " [-1.91797646e-03]\n",
      " [ 1.51719446e+00]\n",
      " [-3.84870717e-01]\n",
      " [ 3.89735650e-01]\n",
      " [ 8.40673613e-02]\n",
      " [ 4.65837205e-01]\n",
      " [-4.42296261e-02]\n",
      " [-2.32728399e-01]\n",
      " [ 7.28889113e-01]\n",
      " [ 1.22027297e+00]\n",
      " [-1.30336319e+00]\n",
      " [-4.54013408e-01]\n",
      " [-1.93169503e-01]\n",
      " [-1.27963203e+00]\n",
      " [ 1.42976314e+00]\n",
      " [ 8.35913580e-02]\n",
      " [ 1.85172769e+00]\n",
      " [ 1.90477523e-01]\n",
      " [-6.32126916e-01]\n",
      " [-2.45093741e+00]\n",
      " [ 4.29641028e-01]\n",
      " [ 2.88100470e-01]\n",
      " [ 7.38323027e-01]\n",
      " [ 1.03458837e+00]\n",
      " [-9.42213135e-01]\n",
      " [-7.02500288e-01]\n",
      " [ 8.13058962e-01]\n",
      " [-3.15333631e-01]\n",
      " [-1.75404975e+00]\n",
      " [-3.68679987e-01]\n",
      " [ 7.74244139e-01]\n",
      " [ 1.14095347e-01]\n",
      " [ 3.47748416e-01]]\n",
      "(100, 1)\n"
     ]
    }
   ],
   "execution_count": 38
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
